package com.china.school;

import org.junit.Test;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.SimpleEvaluationContext;

import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class SpringElTest {
    public static void main(String[] args) {

    }

    @Test
    public void test01(){
        ExpressionParser parser = new SpelExpressionParser();

// evaluates to "Hello World"
        String helloWorld = (String) parser.parseExpression("'Hello'' World'").getValue();

// evaluates to "Tony's Pizza"
        String pizzaParlor = (String) parser.parseExpression("'Tony\"\"s Pizza'").getValue();

        double avogadrosNumber = (Double) parser.parseExpression("6.0221415E+23").getValue();

// evaluates to 2147483647
        int maxValue = (Integer) parser.parseExpression("0x7FFFFFFF").getValue();

        boolean trueValue = (Boolean) parser.parseExpression("true").getValue();

        Object nullValue = parser.parseExpression("null").getValue();

        System.out.println(helloWorld);
        System.out.println(pizzaParlor);
        System.out.println(avogadrosNumber);
        System.out.println(maxValue);
        System.out.println(trueValue);
        System.out.println(nullValue == null);
    }

    @Test
    public void test02(){
        // The constructor arguments are name, birthday, and nationality.
        Inventor tesla = new Inventor("Nikola Tesla",new Date(), "Serbian");

        ExpressionParser parser = new SpelExpressionParser();

        Expression exp = parser.parseExpression("name"); // Parse name as an expression
        String name = (String) exp.getValue(tesla);
        System.out.println(name);
    }
        @Test
    public void test03(){
        ExpressionParser parser = new SpelExpressionParser();

// invokes 'getBytes()'
        Expression exp = parser.parseExpression("'Hello World'.bytes");
        byte[] bytes = (byte[]) exp.getValue();
        System.out.println(bytes);

        System.out.println(parser.parseExpression("'Hello World'.concat('!')").getValue());
        System.out.println(parser.parseExpression("new String('hello world').toUpperCase()").getValue());


    }

    @Test
    public void test04(){
        class Simple {
            public List<Boolean> booleanList = new ArrayList<Boolean>();
            public String name;
        }

        Simple simple = new Simple();
        simple.booleanList.add(true);
        ExpressionParser parser = new SpelExpressionParser();
        EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();

// "false" is passed in here as a String. SpEL and the conversion service
// will recognize that it needs to be a Boolean and convert it accordingly.
        parser.parseExpression("booleanList[0]").setValue(context, simple, "false");
        parser.parseExpression("name").setValue( simple, "aaaaa");

// b is false
        Boolean b = simple.booleanList.get(0);

        System.out.println(b);
        System.out.println(simple.name);
    }

    @Test
    public void test05(){
        SpelParserConfiguration config = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE,
                this.getClass().getClassLoader());

        SpelExpressionParser parser = new SpelExpressionParser(config);

        Expression expr = parser.parseExpression("name");

        Inventor message = new Inventor();

        Object payload = expr.getValue(message);
        System.out.println(payload);
    }

    @Test
    public void test06(){
        SpelExpressionParser parser = new SpelExpressionParser( );
        // evaluates to false
        boolean falseValue = parser.parseExpression(
                "'xyz' instanceof T(Integer)").getValue(Boolean.class);

// evaluates to true
        boolean trueValue = parser.parseExpression(
                "'5.00' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);

// evaluates to false
          falseValue = parser.parseExpression(
                "'5.0067' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);
        System.out.println(falseValue);
        System.out.println(trueValue);

    }

}
